package com.base.cn.platform.os.service.manage.join;

import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.manage.join.SysRoleSubjectDao;
import com.base.cn.platform.os.entity.manage.join.SysRoleSubject;
import com.base.cn.platform.os.entity.manage.join.SysUserRole;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色与专业关联的Biz
 *
 * @author s.li
 * @create 2018-03-20 14:46
 */
@Service
public class SysRoleSubjectBiz extends BaseBiz<SysRoleSubject,SysRoleSubjectDao> {
    private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
    @Autowired
    private CustomCacheChannel customCacheChannel;
    @Autowired
    private SysUserRoleBiz sysUserRoleBiz;

    /**
     * 获取角色的关联专业数据ID
     * @param roleIds 角色ID
     * @return
     */
    public List<SysRoleSubject> findSysRoleSubjectListByRoleIds(String roleIds){
        if(StringUtils.isNotEmpty(roleIds)){
            roleIds = StringUtils.subHeadTailString(roleIds,",");
            if(StringUtils.isNotEmpty(roleIds)){
                List<SysRoleSubject> roleSubjectList = this.find(" roleId in ("+roleIds+")",null,null);
                return roleSubjectList;
            }
        }
        return null;
    }

    /**
     * 通过角色ID串查询角色对象的专业列表，查询结果以角色ID为key,value=列表，进行返回
     * @param roleIds 角色ID
     * @return Map<BigDecimal,List<SysRoleSubject>> 分组后的数据
     */
    public Map<BigDecimal,List<SysRoleSubject>> findSysRoleSubjectMapByRoleIds(String roleIds){
        List<SysRoleSubject> roleSubjectList = this.findSysRoleSubjectListByRoleIds(roleIds);
        if(ObjectUtils.isNotEmpty(roleSubjectList)){
            return roleSubjectList.stream().collect(Collectors.groupingBy(e->e.getRoleId()));
        }
        return null;
    }

    /**
     * 查询用户关联的专业数据
     * @param userId 用户ID
     * @return List<SysRoleSubject>
     */
    public List<SysRoleSubject> findSysUserRoleSubjectListByUserId(BigDecimal userId){
        List<SysUserRole> sysUserRoleList = sysUserRoleBiz.findSysUserRoleListByUserId(userId.toString());
        if(ObjectUtils.isNotEmpty(sysUserRoleList)){
            String roleIds = sysUserRoleList.stream().map(e->e.getRoleId().toString()).collect(Collectors.joining(","));
            //通过角色ID串，获取关联的专业数据
            return  this.find(" roleId in ("+roleIds+")",null,null);
        }
        return null;
    }

    /**
     * 管理用户登录成功后台缓存用户的专业权限
     * @param sin 登录KEY
     */
    public void cacheSysUserSubjectData(String sin,BigDecimal userId) {
        String key = sin + CacheConstants.LOGIN_USER_SUBJECT_KEY_SUFFIX;
        //从缓存中获取用户的专业权限
        List<SysRoleSubject> sysRoleSubjectList = (List<SysRoleSubject>)customCacheChannel.get(J2CacheRegion.ONE_DAY_REGION.getKey(),key).getValue();
        if(sysRoleSubjectList==null){
            sysRoleSubjectList = findSysUserRoleSubjectListByUserId(userId);
            if(ObjectUtils.isNotEmpty(sysRoleSubjectList)){
                String strVal = gson.toJson(sysRoleSubjectList);
                List<Map<String,String>> dataList = gson.fromJson(strVal,new TypeToken<List<Map<String,String>>>(){}.getType());
                //缓存一天
                customCacheChannel.set(J2CacheRegion.ONE_DAY_REGION.getKey(),key,dataList);
            }
        }

    }
}
